﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogLib;

namespace AppDataStruct
{
    [Serializable]
    public class SFZInfo
    {
        private string _sfzh = "";
        /// <summary>
        /// 身份证号
        /// </summary>
        public string SFZH
        {
            get { return _sfzh; }
            set { _sfzh = value; }
        }

        private string _xm = "";
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return _xm; }
            set { _xm = value; }
        }

        private string _xb = "";
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex
        {
            get { return _xb; }
            set { _xb = value; }
        }

        private string _xb_code = "";
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex_Code
        {
            get { return _xb_code; }
            set { _xb_code = value; }
        }

        private string _mz = "";
        /// <summary>
        /// 民族
        /// </summary>
        public string Nation
        {
            get { return _mz; }
            set { _mz = value; }
        }

        private string _mz_code = "";
        /// <summary>
        /// 民族
        /// </summary>
        public string Nation_Code
        {
            get { return _mz_code; }
            set { _mz_code = value; }
        }

        private string _csrq = "";
        /// <summary>
        /// 出生日期
        /// </summary>
        public string BirthDate
        {
            get
            {
                return _csrq;
            }
            set
            {
                _csrq = value;
                int year = 0;
                int month = 0;
                int day = 0;
                if (ParseDateString(value, out year, out month, out day))
                {
                    BirthYear = year.ToString();
                    BirthMonth = month.ToString();
                    BirthDay = day.ToString();
                }
                else
                {
                    Log.WriteAsync("The birthday string parse failed.");
                }
            }
        }

        private string _jzdz = "";
        /// <summary>
        /// 地址
        /// </summary>
        public string Address
        {
            get { return _jzdz; }
            set { _jzdz = value; }
        }

        private string _yxrq = "";
        /// <summary>
        /// 起始有效日期
        /// </summary>
        public string ExpiryDate
        {
            get { return _yxrq; }
            set
            {
                _yxrq = value;
                if (value.Contains("-"))
                {
                    //出生日期
                    string[] dateArray = value.Split(
                        new char[] { '-' },
                        StringSplitOptions.RemoveEmptyEntries);
                    if (dateArray.Length == 2)
                    {
                        ExpiryStartDate = dateArray[0].Trim();
                        ExpiryEndDate = dateArray[1].Trim();
                    }
                }
            }
        }

        /// <summary>
        /// 起始有效日期
        /// </summary>
        public string ExpiryStartDate { get; set; }

        /// <summary>
        /// 截止有效日期
        /// </summary>
        public string ExpiryEndDate { get; set; }

        private string _qfjg = "";

        /// <summary>
        /// 签发机关
        /// </summary>
        public string IssueUnit
        {
            get { return _qfjg; }
            set { _qfjg = value; }
        }

        private string _zp = "";
        /// <summary>
        /// 身份证头像照片路径
        /// </summary>
        public string HeaderPicturePath
        {
            get { return _zp; }
            set { _zp = value; }
        }

        private string _sfzzm = "";
        public string SFZZM
        {
            get { return _sfzzm; }
            set { _sfzzm = value; }
        }

        private string _sfzbm = "";
        public string SFZBM
        {
            get { return _sfzbm; }
            set { _sfzbm = value; }
        }

        /// <summary>
        /// 是否是手输身份证号码
        /// </summary>
        public bool IsManualInputNumberVerify
        {
            get;
            set;
        }

        //日期格式 1990-2-7或19900207或1990-02-07
        public string BirthYear { get; private set; }
        public string BirthMonth { get; private set; }
        public string BirthDay { get; private set; }

        public SFZInfo()
        {
            IsManualInputNumberVerify = false;
            BirthDay = "";
        }

        private bool ParseDateString(string dateString, out int year, out int month, out int day)
        {
            year = 0;
            month = 0;
            day = 0;
            try
            {
                if (dateString.Contains("-")
                    || dateString.Contains("/")
                    || dateString.Contains(".")
                    || dateString.Contains("年")
                    || dateString.Contains("月")
                    || dateString.Contains("日"))
                {
                    //出生日期
                    string[] dateArray = dateString.Trim().Split(
                        new string[] { ".", "-", "/", "年", "月", "日" },
                        StringSplitOptions.RemoveEmptyEntries);
                    if (dateArray.Length == 3)
                    {
                        year = int.Parse(dateArray[0]);
                        month = int.Parse(dateArray[1]);
                        day = int.Parse(dateArray[2]);
                        return true;
                    }
                }
                else if (dateString.Length == 8)
                {
                    string strYear = dateString.Substring(0, 4);
                    string strMonth = dateString.Substring(4, 2);
                    string strDay = dateString.Substring(6, 2);
                    year = int.Parse(strYear);
                    month = int.Parse(strMonth);
                    day = int.Parse(strDay);
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 复姓字
        /// </summary>
        private static string[] MultiName = new string[]
        {
            "欧阳","太史","端木","上官","司马","东方","独孤","南宫","万俟","闻人","夏侯","诸葛","尉迟","公羊","赫连","澹台",
            "皇甫","宗政","濮阳","公冶","太叔","申屠","公孙","慕容","仲孙","钟离","长孙","宇文","城池","司徒","鲜于","司空",
            "闾丘","子车","亓官","司寇","巫马","公西","颛孙","壤驷","公良","漆雕","乐正","宰父","谷梁","拓跋","夹谷","轩辕",
            "令狐","段干","百里","呼延","东郭","南门","羊舌","微生","公户","公玉","公仪","梁丘","公仲","公上","公门","公山",
            "公坚","左丘","公伯","西门","公祖","第五","公乘","贯丘","公皙","南荣","东里","东宫","仲长","子书","子桑","即墨",
            "达奚","褚师"
        };

        public int GetXingAndMing(string xm, out string xing, out string ming)
        {
            xing = "";
            ming = "";
            if (string.IsNullOrEmpty(xm))
            {
                return -1;
            }
            if (xm.Length >= 2)
            {
                if (xm.Length == 2)
                {
                    xing = xm[0].ToString();
                    ming = xm[1].ToString();
                    return 0;
                }
                else
                {
                    string fx = xm.Substring(0, 2);
                    if (MultiName.Contains(fx))
                    {
                        xing = fx;
                        ming = xm.Substring(2);
                        return 0;
                    }
                    else
                    {
                        xing = xm[0].ToString();
                        ming = xm.Substring(1);
                        return 0;
                    }
                }
            }
            return -2;
        }
    }
}
